import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

import java.util.Arrays;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by sToa on 2014/12/26.
 */
public class WhenTestUtils {

    public static boolean exist(String condVal, Map<String, String> dataMap)
    {
        boolean b = false;
        condVal = ParserHelper.trim(condVal);
        if(dataMap == null || dataMap.get(condVal) == null)
        {
            return b;
        }
        return !StringUtils.isEmpty(dataMap.get(condVal).toString());
    }

    public static boolean not_exist(String condVal, Map<String, String> dataMap)
    {
        return !exist(condVal, dataMap);
    }

    public static boolean equal(String key, String val, Map<String, String> dataMap)
    {
        boolean b = false;
        key = ParserHelper.trim(key);
        if(dataMap == null || dataMap.get(key) == null)
        {
            return b;
        }
        b = (dataMap.get(key).toString().toLowerCase()).equals(val.toLowerCase());
//        System.out.println("=: " + key + " " + val + " " + b);
        return b;
    }

    public static boolean unequal(String key, String val, Map<String, String> dataMap)
    {
        return !equal(key, val, dataMap);
    }

    public static boolean matches(String input, String regex, Map<String, String> dataMap)
    {
        boolean b = false;
        input = ParserHelper.trim(input);
        if(dataMap == null || dataMap.get(input) == null || regex == null)
        {
            return b;
        }
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(dataMap.get(input).toString());
        b = m.find();
//        System.out.println("matches: " + input + " " + regex + " " + b);
        return b;
    }

    public static boolean not_matches(String input, String regex, Map<String, String> dataMap)
    {
        return !matches(input, regex, dataMap);
    }

    public static boolean in(String input, String[] paramArr, Map<String, String> dataMap)
    {
        boolean b = false;
        input = ParserHelper.trim(input);
        if(dataMap == null || dataMap.get(input) == null)
        {
            return b;
        }
        int index = ArrayUtils.indexOf(paramArr, dataMap.get(input).toString());
        if(index >= 0)
        {
            b = true;
        } else
        {
            b = false;
        }
//        System.out.println("IN: " + input +" "+ dataMap.get(input).toString()+" " + Arrays.asList(paramArr) + " " +b);
        return b;
    }

    public static void main(String[] args) {
        String[] paramArr = {"Q", "U"};
        System.out.println(Arrays.asList(paramArr)+" "+ArrayUtils.indexOf(paramArr, "Q"));
    }

}
